import { imgstr } from "./imgcode";
async function checkUserName(user) {
  const status = await fetch(
    "https://ad.jxzyz.cn/vol/index.php/Home/Index/checkuser.html",
    {
      headers: {
        accept: "text/plain, */*; q=0.01",
        "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
        "content-type": "application/x-www-form-urlencoded",
        "x-requested-with": "XMLHttpRequest",
      },
      referrer: "https://ad.jxzyz.cn/vol/index.php/Home/Index/zc.html",
      referrerPolicy: "strict-origin-when-cross-origin",
      body: `key=${user}`,
      method: "POST",
      mode: "cors",
      credentials: "include",
    }
  )
    .then((r) => r.json())
    .then((json) => json.status);
  return status;
}

async function checkID(id) {
  //身份证检查
  const status = await fetch(
    "https://ad.jxzyz.cn/vol/index.php/Home/Index/checksfz.html",
    {
      headers: {
        accept: "text/plain, */*; q=0.01",
        "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
        "content-type": "application/x-www-form-urlencoded",
        "x-requested-with": "XMLHttpRequest",
      },
      referrer: "https://ad.jxzyz.cn/vol/index.php/Home/Index/zc.html",
      referrerPolicy: "strict-origin-when-cross-origin",
      body: `key=${id}`,
      method: "POST",
      mode: "cors",
      credentials: "include",
    }
  )
    .then((r) => r.json())
    .then((json) => json.status); // {"status":2} --> 已被使用 1-->ok
  return status;
}

async function checkPhone(p) {
  // 手机号检查
  const status = await fetch(
    "https://ad.jxzyz.cn/vol/index.php/Home/Index/checkmobile.html",
    {
      headers: {
        accept: "text/plain, */*; q=0.01",
        "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
        "content-type": "application/x-www-form-urlencoded",
        "x-requested-with": "XMLHttpRequest",
      },
      referrer: "https://ad.jxzyz.cn/vol/index.php/Home/Index/zc.html",
      referrerPolicy: "strict-origin-when-cross-origin",
      body: `key=${p}`,
      method: "POST",
      mode: "cors",
      credentials: "include",
    }
  )
    .then((r) => r.json())
    .then((json) => json.status);
  return status;
}

async function insertUser(userObj) {
  let {
    userName,
    userPassword,
    email,
    birthCountry,
    realName,
    idcardType,
    idcardCode,
    gender,
    birthday,
    spoliticalStatus,
    national,
    mobile,
    telephone,
    pro,
    city,
    area,
    town,
    steam,
    cityteam,
    areateam,
    zzinfo,
    zzinfo_s,
    address,
    postcode,
    education,
    school,
    employment,
    zc,
    servicemode,
    serviceWeek,
    serviceDay,
    serviceObject,
    serviceDistrict,
    serviceType,
    merit,
    x,
    y,
    w,
    h,
  } = userObj;

  let result = await fetch(
    "https://ad.jxzyz.cn/vol/index.php/Home/Index/userinsert.html",
    {
      credentials: "include",
      headers: {
        accept:
          "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "max-age=0",
        "content-type": "application/x-www-form-urlencoded",
        "upgrade-insecure-requests": "1",
      },
      referrer: "https://ad.jxzyz.cn/vol/index.php/Home/Index/zc.html",
      referrerPolicy: "no-referrer-when-downgrade",
      body: `userName=${userName}&userPassword=${userPassword}&passWordAgain=${userPassword}&email=${email}&birthCountry=${birthCountry}&realName=${realName}&idcardType=${idcardType}&idcardCode=${idcardCode}&gender=${gender}&birthday=${birthday}&spoliticalStatus=${spoliticalStatus}&national=${national}&mobile=${mobile}&telephone=${telephone}&pro=${pro}&city=${city}&area=${area}&town=${town}&steam=${steam}&cityteam=${cityteam}&areateam=${areateam}&zzinfo=${zzinfo}&zzinfo_s=${zzinfo_s}&address=${address}&postcode=${postcode}&education=${education}&school=${school}&employment=${employment}&zc=${zc}&servicemode=${servicemode}&serviceWeek%5B%5D=${serviceWeek}&serviceDay%5B%5D=${serviceDay}&serviceObject%5B%5D=${serviceObject}&serviceDistrict%5B%5D=${serviceDistrict}&serviceType%5B%5D=${serviceType}&merit%5B%5D=${merit}&x=${x}&y=${y}&w=${w}&h=${h}&imgstr=${imgstr}`,
      method: "POST",
      mode: "cors",
    }
  )
    .then((resp) => resp.text())
    .then((resp) => {
      if (resp.includes("face_ok.png")) {
        return 1; // 1 is ok for check-* function also does so.
      } else {
        // registered performed but not good=====================================
        let html = cheerio.load(resp);
        let errormessage = html("p.error").text().trim();
        console.log(errormessage);
        item.registererrormessage = errormessage;
        item.registererror = 1;
      }
    });
  return result;
}
//obj:{community, name,idnumber,username,telephone,politicalStatus}
function extendObj(obj, rename) {
  let age = new Date().getFullYear() - parseInt(obj.idnumber.slice(6, 4));
  obj.age = age;

  let userName = `${obj.username ? obj.username : "y" + obj.telephone}`; //obj.username = userName
  let userPassword = "123456789"; //obj.password = userPassword
  let email = "253686185%40qq.com"; //obj.email = email
  let birthCountry = 156;
  let realName = encodeURI(obj.name); // "%E5%BC%A0%E9%92%A2%E9%94%8B" /////////////////张钢锋
  let idcardType = 1;
  let idcardCode = obj.idnumber; //"360521196009140017" ////////////////
  let gender = parseInt(obj.idnumber.slice(17, 1)) % 2 == 0 ? 2 : 1; //1  ////////////////
  const byear = obj.idnumber.slice(6, 4);
  const bmounth = obj.idnumber.slice(10, 2);
  const bday = obj.idnumber.slice(12, 2);
  let birthday = `${byear}-${bmounth}-${bday}`; //"1960-09-14" /////////////
  let spoliticalStatus = "13"; // 01 党员    02 预备党员 03 共青团员  13 群众
  if (obj.politicalStatus === "党员") {
    spoliticalStatus = "01";
  } else if (age < 16 && age > 10) {
    spoliticalStatus = "03";
    obj.politicalStatus = "共青团员";
  } else {
    obj.politicalStatus = "群众";
  }

  let national = 20;
  let mobile = obj.telephone; //"13217088664"  /////////////
  let telephone = obj.telephone; //"13217088664"  ////////////////
  let pro = 1; // 江西省
  let city = 5; //新余
  let area = 37; //分宜县
  let town = 479; //钤西街道
  let steam = 5; //社区志愿服务总队
  let cityteam = 53; //新余市社区志愿服务支队
  let areateam = 468; //分宜县社区志愿服务大队
  let zzinfo = "50965|分宜县钤西街道办事处新时代文明实践所|4";
  let zzinfo_s = "59146|钤西街道泉塘社区新时代文明实践站|4";
  if (obj.community.includes("泉塘")) {
    zzinfo_s = encodeURIComponent("59146|钤西街道泉塘社区新时代文明实践站|4");
  } else if (obj.community.includes("保健")) {
    zzinfo_s = encodeURIComponent("2678|钤西街道保健巷社区新时代文明实践站|4");
  } else if (obj.community.includes("政法")) {
    zzinfo_s = encodeURIComponent("10545|钤西街道政法巷社区新时代文明实践站|4");
  } else if (obj.community.includes("文教")) {
    zzinfo_s = encodeURIComponent(
      "12146|分宜县钤西街道文教巷社区新时代文明实践站|4"
    );
  } else if (obj.community.includes("锻压厂")) {
    zzinfo_s = encodeURIComponent("16511|钤西街道锻压厂社区新时代文明实践站|4");
  } else if (obj.community.includes("青山")) {
    zzinfo_s = encodeURIComponent("36188|钤西街道青山社区新时代文明实践站|4");
  } else if (obj.community.includes("府前")) {
    zzinfo_s = encodeURIComponent("50973|钤西街道府前社区新时代文明实践站|4");
  } else if (obj.community.includes("分宜矿")) {
    zzinfo_s = encodeURIComponent("39835|钤西街道分宜矿社区新时代文明实践站|4");
  }
  let address = "";
  let postcode = "";
  let education = 30; // 0 博士研究生 1 硕士研究生 10 大学本科 20 大学专科 24 中等专科 25 职业高中 26 技工学校 27 高中 28 初中 29 小学 30 其他
  let school = "杨桥中学"; //"%E6%9D%A8%E6%A1%A5%E4%B8%AD%E5%AD%A6" //杨桥中学

  let sa = [
    "分宜中学",
    "分宜二中",
    "杨桥中学",
    "钤峰中学",
    "分宜一小",
    "分宜二小",
  ];
  if (age > 7 && age < 13) {
    sa = ["分宜一小", "分宜二小"];
    school = sa[Math.floor(Math.random() * sa.length)];
    education = 29;
  } else if (age >= 13 && age < 16) {
    sa = ["分宜中学", "分宜二中", "杨桥中学", "钤峰中学"];
    school = sa[Math.floor(Math.random() * sa.length)];
    education = 28;
  } else if (age >= 16 && age < 19) {
    sa = ["分宜中学", "分宜二中"];
    school = sa[Math.floor(Math.random() * sa.length)];
    education = 27;
  } else if (age >= 19 && age < 45) {
    sa = ["分宜中学", "分宜二中"];
    school = sa[Math.floor(Math.random() * sa.length)];
    let choose = [26, 27];
    education = choose[Math.floor(Math.random() * choose.length)];
  } else if (age >= 45 && age < 65) {
    sa = [
      "分宜中学",
      "分宜二中",
      "杨桥中学",
      "钤峰中学",
      "分宜一小",
      "分宜二小",
    ];
    school = sa[Math.floor(Math.random() * sa.length)];
    let choose = [28, 29, 30];
    education = choose[Math.floor(Math.random() * choose.length)];
  } else if (age >= 65) {
    sa = ["分宜一小", "分宜二小"];
    school = sa[Math.floor(Math.random() * sa.length)];
    let choose = [29, 30];
    education = choose[Math.floor(Math.random() * choose.length)];
  }
  school = encodeURI(school);

  let employment = 90; // 11 国家公务员 17 职员 24 工人 27 农民 31 学生 51 自由职业者 54 个体经营者 70 无业人员 90 其他 80 退（离）休人员
  let zc = "001"; //职称// 001 其他
  let servicemode = "01"; //  01 现场服务
  let serviceWeek = 2; // serviceWeek%5B%5D=2  可服务时间 1 星期一 2 星期二 。。。
  let weekdays = [1, 2, 3, 4, 5, 6, 7];
  serviceWeek = weekdays[Math.floor(Math.random() * weekdays.length)];

  let serviceDay = 3; // serviceDay%5B%5D=3  可服务时段 1 早上 2下午 3晚上
  let daytimes = [1, 2, 3];
  serviceDay = daytimes[Math.floor(Math.random() * daytimes.length)];

  let serviceObject = 4; // serviceObject%5B%5D=4 服务对象  1 青少年   2 婴幼儿   3 孤寡老人   4 残障人士   5 优抚对象   6 特困群体   7 其它
  serviceObject = weekdays[Math.floor(Math.random() * weekdays.length)];

  // let serviceObject=6 // serviceObject%5B%5D=6
  let serviceDistrict = 5; // serviceDistrict%5B%5D=5  服务区域  5 新余市
  let serviceType = 17; // serviceType%5B%5D=17服务类别 1 法律咨询  2维权服务 3心理健康 4敬老助老 5文化娱乐 6全民健身 7治安巡逻 8环保绿化 9卫生保洁 10教育培训
  ///    11 纠纷调解  12消防安全  13扶贫济困  14科技创新  15电器维修  16医疗保健  17其它
  let merit = 25; // merit%5B%5D=25 专长特长  1英语    2书法    3绘画    4摄影    5电脑维修    6文化辅导    7编辑写作    8话剧演讲    9节目主持    10电脑编程
  //// 11漫画    12唱歌    13舞蹈    14钢琴    15推拿按摩    16创意策划    17处理应变    18社交外联    19人事管理    20平面设计    21吉他
  ///  22护理    23网页制作    24手工制作    25其他
  let x = 0;
  let y = 0;
  let w = 100;
  let h = 100;

  let passobject = {
    userName,
    userPassword,
    email,
    birthCountry,
    realName,
    idcardType,
    idcardCode,
    gender,
    birthday,
    spoliticalStatus,
    national,
    mobile,
    telephone,
    pro,
    city,
    area,
    town,
    steam,
    cityteam,
    areateam,
    zzinfo,
    zzinfo_s,
    address,
    postcode,
    education,
    school,
    employment,
    zc,
    servicemode,
    serviceWeek,
    serviceDay,
    serviceObject,
    serviceDistrict,
    serviceType,
    merit,
    x,
    y,
    w,
    h,
  };
  return passobject;
}
function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
export { checkUserName, checkID, checkPhone, extendObj, insertUser, sleep };
